#include <graphics.h>

#include "animation.h"



int idx_current_anim = 0;
const int PLAYER_ANIM_NUM = 6;

IMAGE img_player_left[PLAYER_ANIM_NUM];
IMAGE img_player_right[PLAYER_ANIM_NUM];

POINT player_pos = {500, 500};
int PLAYER_SPEED = 6;

//Animation anim_left_play(_T("img/player_left_%d.png"), 6, 45);
//Animation anim_right_play(_T("img/player_right_%d.png"), 6, 45);
int main()
{
     initgraph(1280, 720);
     bool running = true;
     ExMessage msg;
     IMAGE img_background;
     bool is_move_up = false;
     bool is_move_down = false;
     bool is_move_left = false;
     bool is_move_right = false;
     loadimage(&img_background, _T("../img/background.png"));

     BeginBatchDraw();
     while (running)
     {
          DWORD start_time = GetTickCount();
          while (peekmessage(&msg))
          {
               if (msg.message == WM_KEYDOWN)
               {
                    switch (msg.vkcode)
                    {
                    case VK_UP:
                         is_move_up = true;
                         break;
                    case VK_DOWN:
                         is_move_down = true;
                         break;
                    case VK_LEFT:
                         is_move_left = true;
                         break;
                    case VK_RIGHT:
                         is_move_right = true;
                         break;
                    }
               }
               else if (msg.message == WM_KEYUP)
               {
                    switch (msg.vkcode)
                    {
                    case VK_UP:
                         is_move_up = false;
                         break;
                    case VK_DOWN:
                         is_move_down = false;
                         break;
                    case VK_LEFT:
                         is_move_left = false;
                         break;
                    case VK_RIGHT:
                         is_move_right = false;
                         break;
                    }
               }
          }
          if (is_move_up)
          {
               player_pos.y -= PLAYER_SPEED;
          }
          if (is_move_down)
          {
               player_pos.y += PLAYER_SPEED;
          }
          if (is_move_left)
          {
               player_pos.x -= PLAYER_SPEED;
          }
          if (is_move_right)
          {
               player_pos.x += PLAYER_SPEED;
          }
          // 静态局部变量：在函数内部声明的静态变量。即使函数结束，静态局部变量的值也会保留
          static int counter = 0;
          if (++counter % 5 == 0)
          {
               idx_current_anim++;
          }
          // 使用动画循环播放
          idx_current_anim = idx_current_anim % PLAYER_ANIM_NUM;
          cleardevice();

          putimage(0, 0, &img_background);
          // putimage_alpha(player_pos.x, player_pos.y, &img_player_left[idx_current_anim]);
          FlushBatchDraw();

          DWORD end_time = GetTickCount();
          DWORD delta_time = end_time - start_time;
          if (delta_time < 1000 / 60)
          {
               Sleep(1000 / 60 - delta_time);
          }
     }
     EndBatchDraw();
}